package com.ysyx.ysyx_passengerinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ysyx.ysyx_passengerinfo.dto.BaseDto;
import com.ysyx.ysyx_passengerinfo.mapper.OrderPojoMapper;
import com.ysyx.ysyx_passengerinfo.pojo.PassengerPojo;
import com.ysyx.ysyx_passengerinfo.qo.PassengerQo;
import com.ysyx.ysyx_passengerinfo.qo.TakeOrderQo;
import com.ysyx.ysyx_passengerinfo.service.PassengerPojoService;
import com.ysyx.ysyx_passengerinfo.mapper.PassengerPojoMapper;
import com.ysyx.ysyx_passengerinfo.util.SnowFlakeUtil;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 薛宇晖
* @description 针对表【passenger_inf(乘客信息表)】的数据库操作Service实现
* @createDate 2023-04-07 14:19:08
*/
@Service
public class PassengerPojoServiceImpl
    implements PassengerPojoService{

    @Autowired
    private PassengerPojoMapper passengerPojoMapper;

    @Autowired
    private OrderPojoMapper orderPojoMapper;

    private RedissonClient redissonClient;

    @Override
    public BaseDto LoginByAcc(PassengerQo qo) {
        BaseDto baseDto = new BaseDto();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("passenger_acc",qo.getPassengerAcc());
//        queryWrapper.eq("passenger_pwd",qo.getPwd());
        List<PassengerPojo> passengerPojos = new ArrayList<>();
        passengerPojos = passengerPojoMapper.selectList(queryWrapper);

        if (passengerPojos.size()==0){
            baseDto = new BaseDto(201,"账号不存在",passengerPojos);
            return baseDto;
        }
        if (passengerPojos.get(0).getPassengerPwd().equals(qo.getPassengerPwd())) {
            baseDto = new BaseDto(102,"成功登录",passengerPojos);








            return baseDto;
        }else {
            baseDto = new BaseDto(101,"账号密码错误",passengerPojos);
            return baseDto;
        }

    }

    @Override
    public BaseDto LoginByTel(PassengerQo qo) {

        BaseDto baseDto = new BaseDto();
        QueryWrapper  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("passenger_tel",qo.getPassengerTel());
        List<PassengerPojo> list = passengerPojoMapper.selectList(queryWrapper);
        if (list.size()==0){

            //随机生成一个账号给他帮他注册

            String fid = String.valueOf(SnowFlakeUtil.getSnowFlakeId());
            PassengerPojo p = new PassengerPojo(0,fid,fid,"ysyx用户","null",qo.getPassengerTel(),1,null,null);
            passengerPojoMapper.insert(p);
            list.add(p);
            baseDto = new BaseDto(101,"此手机号未被注册,现在已经帮他注册",list);

            return  baseDto;
        }

        //获取redis存这的验证码
        RBucket<Object> bucket = redissonClient.getBucket("login:code:" + qo.getPassengerTel());
        String o = (String) bucket.get();
        if (qo.getCode().equals(o)){
            //校验输入验证码
            baseDto = new BaseDto(102,"登录成功",list);

        }else {
            baseDto = new BaseDto(103,"验证码错误",list);
        }
        return baseDto;
    }

    @Override
    public BaseDto LoginByOpenId(PassengerQo qo) {
        return null;
    }

    @Override
    public BaseDto selectPassengerByPassengerId(Integer passengerId) {
        QueryWrapper<PassengerPojo> passengerQw = new QueryWrapper<>();
        passengerQw.eq("passenger_id",passengerId);
        List<PassengerPojo> passengerPojos = passengerPojoMapper.selectList(passengerQw);

        if(passengerPojos.size()==0){
            return new BaseDto(201,"乘客信息不存在",null);
        }else{
            return new BaseDto(101,"乘客信息查询成功",passengerPojos);
        }
    }

    @Override
    public BaseDto selectAllPassenger() {
        QueryWrapper<PassengerPojo> passengerQw = new QueryWrapper<>();
        List<PassengerPojo> passengerPojos = passengerPojoMapper.selectList(passengerQw);
        return new BaseDto(101,"乘客信息查询成功",passengerPojos);
    }

    @Override
    public BaseDto selectAllOrder(Integer passengerId) {
        List<TakeOrderQo> takeOrderQos = orderPojoMapper.selectAllTakeOrderQoByPassengerId(passengerId);
        return new BaseDto(0,null,takeOrderQos);
    }

    @Override
    public BaseDto passengerReg(PassengerPojo pojo) {
        //开始乘客注册
        QueryWrapper<PassengerPojo> passengerPojoQueryWrapper = new QueryWrapper<>();
        passengerPojoQueryWrapper.eq("passenger_acc",pojo.getPassengerAcc());
        List<PassengerPojo> passengerPojos = passengerPojoMapper.selectList(passengerPojoQueryWrapper);
        if (passengerPojos.size()>0) {
            return new BaseDto(201,"存在相同账号的比",null);
        }
        QueryWrapper<PassengerPojo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("passenger_name",pojo.getPassengerName());
        List<PassengerPojo> passengerPojos1 = passengerPojoMapper.selectList(queryWrapper);
        if (passengerPojos1.size()>0) {
            return new BaseDto(202,"存在相同昵称的比",null);
        }
        QueryWrapper<PassengerPojo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("passenger_tel",pojo.getPassengerTel());
        List<PassengerPojo> passengerPojos2 = passengerPojoMapper.selectList(queryWrapper1);
        if (passengerPojos2.size()>0) {
            return new BaseDto(203,"存在相同的手机号",null);
        }

        QueryWrapper queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("passenger_idno",pojo.getPassengerIdno());
        List<PassengerPojo> list = passengerPojoMapper.selectList(queryWrapper2);
        if (list.size()>0) {
            return new BaseDto(204,"身份证已被注册",null);
        }
        pojo.setPassengerState(1);

        int insert = passengerPojoMapper.insert(pojo);
        if (insert==1){
            return new BaseDto(101,"成功注册",null);
        }else {
            return new BaseDto(205,"注册失败",null);
        }

    }

    @Override
    public BaseDto passengerFixPwd(PassengerPojo pojo) {

        QueryWrapper<PassengerPojo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("passenger_acc",pojo.getPassengerAcc());
        queryWrapper.eq("passenger_pwd",pojo.getPassengerTel());
        List<PassengerPojo> passengerPojos = passengerPojoMapper.selectList(queryWrapper);
        if (passengerPojos.size()==0) {
            return new BaseDto(201,"不存在此账号，想殴打杰荣",null);
        }

        UpdateWrapper<PassengerPojo> driverUw = new UpdateWrapper<>();
        driverUw.eq("passenger_acc",pojo.getPassengerAcc()).set("passenger_pwd",pojo.getPassengerPwd());
        int update = passengerPojoMapper.update(null, driverUw);
        if (update==1){
            return new BaseDto(101,"成功修改",null);
        }


        return new BaseDto(202,"更新失败，检查参数",null);
    }





}




